package com.smt.take.ui.activity

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.smt.take.R
import com.smt.take.model.beans.RecepitAddressBean
import com.smt.take.model.dao.AddressDao
import com.smt.take.ui.adapter.AddressRvAdapter
import com.smt.take.ui.views.RecycleViewDivider
import kotlinx.android.synthetic.main.activity_address_list.*
import kotlinx.android.synthetic.main.activity_order_detail.*

class RecepitAddressActivity : BaseActivity() {

    lateinit var addressDao: AddressDao
    lateinit var adapter: AddressRvAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_address_list)
        // 判断设备是否有虚拟按键
        if (checkDeviceHasNavigationBar(this)) {
            activity_address_list.setPadding(0, 0, 0, 48.dp2px())
        }
        addressDao = AddressDao(this)
        rv_receipt_address.layoutManager = LinearLayoutManager(this)
        rv_receipt_address.addItemDecoration(
            RecycleViewDivider(
                this,
                LinearLayoutManager.HORIZONTAL
            )
        )
        adapter = AddressRvAdapter(this)
        rv_receipt_address.adapter = adapter
        ib_back.setOnClickListener { finish() }
        tv_add_address.setOnClickListener {
            val intent = Intent(this, AddOrEditAddressActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onStart() {
        super.onStart()
        val addressList = addressDao.queryAllAddress()
        if (addressList.isNotEmpty()) {
            // toast("一共有" + addressList.size + "个地址")
            adapter.setAddList(addressList as ArrayList<RecepitAddressBean>)
        }
    }
}